<template>
  <div class="root">
    <div class="headerBox">
      <div class="backBox" @click="$router.back()">
        <i class="el-icon-arrow-left"></i>
        返回
      </div>
      <div class="CurrentPosition">
        当前位置： 管理后台 >> 个人资料 >>
        <span class="redBox">编辑个人信息</span>
      </div>
    </div>
    <div class="titleBox">
      如需修改登录账号信息，可联系平台客服为您修改处理。
    </div>
    <div class="formBox">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="微信号：">
          <el-input v-model="form.vxNumber" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="QQ号：">
          <el-input v-model="form.qqNumber" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="头像：">
          <el-upload
            :action="action + '/common/upload/file'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            list-type="picture-card"
          >
          <div class="cententBox" v-if="form.icon==''">
              <img
                src="@/static/Imgs/userImg/shangchuan.png"
                class="uploadImg"
                alt=""
              />
              <div>上传图片</div>
            </div>
            <div class="cententBox" v-else>
              <img
              style="width: 1.48rem;height: 1.48rem;"
                :src="form.icon"
                alt=""
              />
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="">
          <div class="redBtn" @click="updateInfo">确认修改</div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
    
    <script>
import { updateInfo,getUserData } from "@/api/user";
import imgPath from "@/utils/config";
export default {
  data() {
    const validatesjh = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入手机号"));
      } else if (!sjhreg.test(value)) {
        callback(new Error("请检查手机号码是否正确"));
      } else {
        callback();
      }
    };
    const sjhreg =
      /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    return {
      form: {
        icon:''
      },
      action: "",
      rules: {
        phone: [
          { validator: validatesjh, trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.action = imgPath.imgPath;
    this.getUserData();
  },
  methods: {
     // 获取个人信息
     getUserData() {
      getUserData().then((data) => {
        this.form = data.data;
        if(!this.form.icon){
          this.form.icon=''
        }
      });
    },
    //修改用户信息
    updateInfo(){
      this.form.id=this.$route.query.id
      updateInfo(this.form).then(data=>{
        if(data.code==200){
          this.$message({
                message: "修改成功",
                type: "success",
              });
              this.$router.back()
        }
      })
    },
    //上传头像
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.form.icon = res.data;
    },
    //图片校验
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 3;
      if (!isLt2M) {
        this.$message.error("上传附件大小不能超过 3MB!");
        return false;
      }
      const isJPEG = file.type === "image/jpeg";
      const isJPG = file.type === "image/jpg";
      const isPNG = file.type === "image/png";
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const isPDF = testmsg === "pdf";
      if (!isJPEG && !isJPG && !isPNG && !isPDF) {
        this.$message.error("上传图片只能是 png,jpg,pdf 格式!");
        return false;
      }
      this.loading = true;
      return true;
    },
  },
};
</script>
    
    <style scoped lang="scss">
    
.root {
  .headerBox {
    display: flex;
    align-items: center;
    .backBox {
      width: 0.7rem;
      height: 0.3rem;
      line-height: 0.3rem;
      text-align: center;
      font-size: 0.12rem;
      border: 1px solid #e8e8e8;
      border-top-left-radius: 50px;
      border-bottom-left-radius: 50px;
      cursor: pointer;
    }
    .CurrentPosition {
      display: flex;
      font-size: 0.12rem;
      color: #3d3d3d;
      margin-left: 0.3rem;
      .redBox {
        color: #e22a1f;
      }
    }
  }
  .titleBox {
    color: #ec6c15;
    font-size: 0.14rem;
    margin-top: 0.2rem;
  }
  .formBox {
    margin-top: 0.2rem;
    /deep/ .el-form-item {
      display: flex;
      margin-bottom: 0.2rem;
    }
    /deep/ .el-input__inner {
      background-color: #f5f7f9;
      border: 1px solid #e8e8e8;
    }
    /deep/ .el-input {
      width: 3rem;
    }
    .uploadImg {
      width: 0.4rem;
      height: 0.4rem;
      margin: 0.2rem 0;
      margin-top: 0.3rem;
    }
    .cententBox {
      color: #999999;
    }
    /deep/ .el-upload--picture-card {
      line-height: 0;
    }
    .redBtn {
      width: 0.9rem;
      height: 0.3rem;
      line-height: 0.3rem;
      background: #e22a1f;
      color: #fff;
      border-radius: 20px;
      text-align: center;
      margin-left: 0.8rem;
      margin-top: 0.2rem;
      cursor: pointer;
    }
  }
}
</style>
    
    